<template>
  <div class="app-container">
    <!-- <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="课程年份">
        <el-date-picker
          v-model="queryParams.courseYear"
          type="year"
          placeholder="请选择课程年份">
        </el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form> -->

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['school:sccourse:add']"
          >新增</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['school:sccourse:export']"
          >导出</el-button
        >
      </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <div class="table">
      <div class="header">
        <div class="selection_header CENTER">
          <el-checkbox :indeterminate="isIndeterminate" v-model="checkAll" @change="handleCheckAllChange"></el-checkbox>
        </div>
        <div class="week CENTER" style="flex:2.5;">课程</div>
        <div class="week CENTER" style="flex:1;">周一</div>
        <div class="week CENTER" style="flex:1;">周二</div>
        <div class="week CENTER" style="flex:1;">周三</div>
        <div class="week CENTER" style="flex:1;">周四</div>
        <div class="week CENTER" style="flex:1;">周五</div>
        <div class="week CENTER" style="flex:1;">周六</div>
        <div class="week CENTER" style="flex:1;">周天</div>
        <div class="week control CENTER" style="flex:1;">操作</div>
      </div>
      <div class="li" style="display: flex;border-bottom: 1px solid #dfe6ec;" v-for="(item, index) in sccourseList" :key="index">
        <div class="selection _public CENTER" style="width: 55px;">
          <el-checkbox v-model="item.ifChecked" @change="handleCheckedChange"></el-checkbox>
        </div>
        <div class="courseName _public CENTER" style="flex:2.5">{{item.courseName}}</div>
        <div class="one _public CENTERY" style="flex:1">
          <div class="courseTime" v-for="(ele, index) in item.mon" :key="index">
            <div class="CENTER public_border_bottom" style="height:100%; min-height: 30px; width: 100%;">{{ele}}</div>
          </div>
        </div>
        <div class="two _public CENTERY" style="flex:1">
          <div class="courseTime" v-for="(ele, index) in item.tue" :key="index">
            <div class="CENTER public_border_bottom" style="height:100%; min-height: 30px; width: 100%;">{{ele}}</div>
          </div>
        </div>
        <div class="three _public CENTERY" style="flex:1">
          <div class="courseTime" v-for="(ele, index) in item.wed" :key="index">
            <div class="CENTER public_border_bottom" style="height:100%; min-height: 30px; width: 100%;">{{ele}}</div>
          </div>
        </div>
        <div class="four _public CENTERY" style="flex:1">
          <div class="courseTime" v-for="(ele, index) in item.thu" :key="index">
            <div class="CENTER public_border_bottom" style="height:100%; min-height: 30px; width: 100%;">{{ele}}</div>
          </div>
        </div>
        <div class="five _public CENTERY" style="flex:1">
          <div class="courseTime" v-for="(ele, index) in item.fri" :key="index">
            <div class="CENTER public_border_bottom" style="height:100%; min-height: 30px; width: 100%;">{{ele}}</div>
          </div>
        </div>
        <div class="six _public CENTERY" style="flex:1">
          <div class="courseTime" v-for="(ele, index) in item.sat" :key="index">
            <div class="CENTER public_border_bottom" style="height:100%; min-height: 30px; width: 100%;">{{ele}}</div>
          </div>
        </div>
        <div class="seven _public CENTERY" style="flex:1">
          <div class="courseTime" v-for="(ele, index) in item.sun" :key="index">
            <div class="CENTER public_border_bottom" style="height:100%; min-height: 30px; width: 100%;">{{ele}}</div>
          </div>
        </div>
        <div class="last_control _public CENTERY" style="flex:1">
          <span @click="pkChangeEdit(item.courseName)"><i class="el-icon-document" style="margin-right:2px;" ></i>查看详情</span>
        </div>
      </div>

    </div>
    <!-- 添加或修改课程对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="课程名字" prop="classId">
          <SearchCourseName ref="SearchCourseName" @update="updateCourseInfo" />

        </el-form-item>
        <el-form-item label="星期" prop="week">
          <el-select v-model="form.week" placeholder="请选择星期">
            <el-option
              v-for="dict in dict.type.sys_week_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="开始小时" prop="startTime">
          <SelectClassTime placeholder="请选择开始时间" ref="SelectClassTimeStart" @change="selectStartTime" />
        </el-form-item>
        <el-form-item label="结束小时" prop="endTime">
          <SelectClassTime placeholder="请选择结束时间" ref="SelectClassTimeEnd" @change="selectEndTime" />
        </el-form-item>
        <el-form-item label="教师" prop="teacherId">
           <SearchTeacher ref="SearchTeacherName" @change="updateTeacherInfo" /> 

        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getSccourseList,
  getSccourse,
  delSccourse,
  addSccourse,
  updateSccourse,
  exportSccourse,
  getAllTeacher
} from "@/api/school/sccourse";

import SearchCourseName from '../model/searchCourseName'
import SearchTeacher from '../model/searchTeacher'
import SelectClassTime from '../model/selectClassTime'

export default {
  name: "Sccourse",
  dicts: ['sys_week_type'],
  components: {SearchCourseName, SearchTeacher, SelectClassTime},
  data() {
    return {
      isIndeterminate: false,
      checkAll: false,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      selectList: [], // 选择的课程id
      // 课程表格数据
      sccourseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      courseMonthList: [
        {
          value: '0',
          label: '上学期'
        },{
          value: '1',
          label: '下学期'
        }
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        courseYear: null, // 年份
        courseMonth: null, // 学期 0 - 上学期    1 - 下学期
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        classId: [ { required: true, message: "课程名字不能为空", trigger: "blur" } ],
        week: [ { required: true, message: "星期不能为空", trigger: "blur" } ],
        startTime: [ { required: true, message: "开始时间不能为空", trigger: "blur" } ],
        endTime: [ { required: true, message: "结束时间不能为空", trigger: "blur" } ],
        teacherId: [ { required: true, message: "教师不能为空", trigger: "blur" } ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    selectStartTime(e) {
      this.form.startTime = e
    },
    selectEndTime(e) {
      this.form.endTime = e
    },
    updateCourseInfo(e) {
      this.form.classId = e
    },
    updateTeacherInfo(e) {
      console.log("老师的id：",e)
      this.form.teacherId = e
    },
    handleCheckAllChange(val) {
      let countTrue = 0;
      let countFalse = 0;
      this.sccourseList.forEach(item => {
        !item.ifChecked ? countFalse++ : countTrue++
      })
      if (countTrue === this.sccourseList.length) {
        this.checkAll = false;
      }
      if (countFalse === this.sccourseList.length) {
        this.checkAll = true;
      }
      this.isIndeterminate = false;
      this.sccourseList.forEach(item => {
        item.ifChecked = this.checkAll;
      })
      this.$forceUpdate();
    },
    handleCheckedChange() {
      let countTrue = 0;
      let countFalse = 0;
      this.sccourseList.forEach(item => {
        !item.ifChecked ? countFalse++ : countTrue++
      })
      if (countTrue === this.sccourseList.length) {
        this.isIndeterminate = false;
        this.checkAll = true;
        return;
      }
      if (countFalse === this.sccourseList.length) {
        this.isIndeterminate = false;
        this.checkAll = false;
        return;
      }
      this.isIndeterminate = true;
      this.checkAll = false;
      return;
    },
    /** 查询课程列表 */
    getList() {
      this.loading = true;
      getSccourseList(this.queryParams).then((response) => {
        response.data.forEach(item => {
          item.ifChecked = false
        })
          this.sccourseList = response.data;
          this.total = response.total;
          this.loading = false;
        })
        .catch((err) => {
          this.loading = false;
        });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.$refs.SearchCourseName.init()
      this.$refs.SearchTeacherName.init()
      this.$refs.SelectClassTimeStart.init()
      this.$refs.SelectClassTimeEnd.init()
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        week: null,
        startTime: null,
        endTime: null,
        classId: null,
        teacherId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.$nextTick(() => {
        this.$refs.SearchCourseName.init()
        this.$refs.SearchTeacherName.init()
        this.$refs.SelectClassTimeStart.init()
        this.$refs.SelectClassTimeEnd.init()
      })
      this.title = "添加课程";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getSccourse(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.$refs.SearchCourseName.init()
        this.$refs.SearchTeacherName.init()
        this.$refs.SearchCourseName.classId = this.form.classId
        this.title = "修改课程";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          let query = {
            ...this.form,
            startTime: this.form.startTime + ':00',
            endTime: this.form.endTime + ':00'
          }
          if (this.form.id != null) {
            updateSccourse(query).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSccourse(query).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除课程编号为"' + ids + '"的数据项？')
        .then(function () {
          return delSccourse(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal
        .confirm("是否确认导出所有课程数据项？")
        .then(() => {
          this.exportLoading = true;
          return exportSccourse(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
          this.exportLoading = false;
        })
        .catch(() => {});
    },
    //排课跳转
    pkChangeEdit(e){
      console.log("---------------------------")
       this.$router.push({path: `sccoursepk?name=${e}`})
    }
  },
};
</script>

<style scoped lang="scss">
  .CENTER {
    display: flex;
    align-items: center;
    justify-content: center;
  }
  .CENTERY {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
  }
  .table {
    border: 1px solid #dfe6ec;
    border-right: none;
    border-bottom: none;
    font-size: 14px;
    color: #606266;
  }
  .header {
    display: flex;
    height: 49px;
    background: #f8f8f9;
    font-size: 13px;
    color: #333;
    font-weight: 600;
    border-bottom: 1px solid #dfe6ec;
    .selection_header {
      width: 55px;
      height: 100%;
      border-right: 1px solid #dfe6ec;
    }
    .week {
      height: 100%;
      // width: 180px;
      border-right: 1px solid #dfe6ec;
    }
  }
  .courseTime {
    width: 100%;
    flex: 1;
  }
  .CENTERY .courseTime:last-child .CENTER {
    border-bottom: none;
  }
  ._public {
    // width: 180px;
    border-right: 1px solid #dfe6ec;
  }
  .public_border_bottom {
    border-bottom: 1px solid #dfe6ec;
  }
  .last_control {
    font-size: 14px;
    cursor: pointer;
    color: #1890ff;
  }
</style>
